package com.ruoyi.hr.controller.hr.salaryadmin.salarychange;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.hr.domain.Authority;
import com.ruoyi.hr.domain.welfare.tsgjj.Tsgjj;
import com.ruoyi.hr.service.AuthorityService;
import com.ruoyi.hr.service.salaryadmin.salarychange.SalaryChangeService;
import com.ruoyi.hr.util.BaseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 薪资开启
 */
@Controller
@RequestMapping("/salaryChange/salaryChange")
public class SalaryChangeController extends BaseController {

    @Autowired
    SalaryChangeService salaryChangeService;

    @Autowired
    AuthorityService authorityService;

    private String prefix = "hr/salaryadmin/salarychange";

    /**
     * 跳转初始化页面
     */
    @GetMapping()
    public String salaryChange() {
        return prefix + "/salaryChange";
    }

    /**
     * 跳转薪资变动历史页面
     */
    @GetMapping("/salaryChangeHistory")
    public String salaryChangeHistory() {
        return prefix + "/salaryChangeHistory";
    }

    @RequestMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestParam Map map) {
        Authority authority = authorityService.findAuthorAndIsAdmin();
        List<Map<String, Object>> list = salaryChangeService.findData(map, authority);
        return getDataTable(list);
    }

    @RequestMapping("/findDataHistory")
    @ResponseBody
    public TableDataInfo findDataHistory(@RequestParam Map map) {
        Authority authority = authorityService.findAuthorAndIsAdmin();
        startPage();
        List<Map<String, Object>> list = salaryChangeService.findDataHistory(map, authority);
        return getDataTable(list);
    }

    @PostMapping("/updateSalary")
    @ResponseBody
    public int updateSalary(@RequestParam Map map) {
        //项目
        String[] array = map.get("benefitTypeAndXl").toString().split("@@@");
        String stdId = array[1];
        map.put("stdId", stdId);
        return salaryChangeService.updateSalary(map);
    }

    /**
     * 添加员工跳转
     *
     * @return
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 获取添加员工
     *
     * @return
     */
    @RequestMapping("/findChangeEmp")
    @ResponseBody
    public TableDataInfo findChangeEmp(@RequestParam Map map) {

        Authority authority = authorityService.findAuthorAndIsAdmin();
        startPage();
        List<Map<String, Object>> list = salaryChangeService.findChangeEmp(map, authority);
        return getDataTable(list);
    }

    /**
     * 添加员工存储过程
     */
    @PostMapping("/addSalaryChangeEmpProcedure")
    @ResponseBody
    public BaseResult addSalaryChangeEmpProcedure(String allPeid) {
        return salaryChangeService.addSalaryChangeEmpProcedure(allPeid);
    }

    /**
     * 删除添加后的员工存储过程
     */
    @PostMapping("/deleteSalaryChangeEmpProcedure")
    @ResponseBody
    public BaseResult deleteSalaryChangeEmpProcedure(String allId) {
        return salaryChangeService.deleteSalaryChangeEmpProcedure(allId);
    }

    /**
     * 更新薪资数据
     */
    @PostMapping("/updateSalaryDataProcedure")
    @ResponseBody
    public BaseResult updateSalaryData(String allId) {
        return salaryChangeService.updateSalaryDataProcedure(allId);
    }

    /**
     * 数据确认,取消确认,变动处理
     */
    @PostMapping("/threeProcedure")
    @ResponseBody
    public BaseResult threeProcedure(String allId, String type) {
        return salaryChangeService.threeProcedure(allId, type);
    }

    @GetMapping("/updateSalarysxData")
    public String updateSalarysxData(String id, ModelMap mmap) {
        mmap.put("allId", id);
        //使用allId获取所有姓名
        mmap.put("names",salaryChangeService.findNameByAllid(id));
        return prefix + "/updateSalarysxData";
    }

    @PostMapping("/editEffdate")
    @ResponseBody
    public AjaxResult editEffdate(@RequestParam Map map){
        return toAjax(salaryChangeService.editEffdate(map));
    }
}
